﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PlatformDemo
{
    public static class Reflection
    {
        public static List<T> GetAssemblies<T>()
        {
            var types = AppDomain.CurrentDomain.GetAssemblies()
                        .SelectMany(a => a.GetTypes().Where(t => t.GetInterfaces().Contains(typeof(T))))
                        .ToArray().OrderBy(o => o.FullName);

            List<T> _plugins = new List<T>();

            foreach (Type type in types)
                _plugins.Add((T)Activator.CreateInstance(type));

            return _plugins;
        }

        //Plugins = Reflection.GetAssemblies<Interface>();

        public static IEnumerable<T> CreateAllInstancesOf<T>()
        {
            return typeof(T).Assembly.GetTypes()
                .Where(t => typeof(T).IsAssignableFrom(t))
                .Where(t => !t.IsAbstract && t.IsClass)
                .Select(t => (T)Activator.CreateInstance(t));
        }

        //var _plugins = Reflection.CreateAllInstancesOf<Interface>()/*.OrderBy(p => p.Name)*/;

    }
}
